#pragma once
#include <iostream>
#include <cstring>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>

using namespace std;

const char *pathname = "/home/zsw/linux/shm";
const int proj_id = 0x1122;

const int size = 4096;

key_t getkey()
{
    key_t key = ftok(pathname, proj_id);
    if (key < 0)
    {
        std::cerr << "errno: " << errno << ", errstring: " << strerror(errno) << std::endl;
        exit(1);
    }
    return key;
}

int _createshm(key_t key, int size, int flag)
{
    int shmid = shmget(key, size, flag);
    if (shmid < 0)
    {
        std::cerr << "errno: " << errno << ", errstring: " << strerror(errno) << std::endl;
        exit(1);
    }

    return shmid;
}

int createshm(key_t key, int size)
{
    return _createshm(key, size, IPC_CREAT | IPC_EXCL | 0644);

}

int getshm(key_t key)
{
    return _createshm(key, size, 0);
}